<template>
  <el-form :model="apply" label-width="100px" class="demo-ruleForm">
    <h3>申请人：{{name}}</h3>
    <h3>寝室号：{{dorid}}</h3>
    <el-form-item label="选择申请类型">
        <el-select v-model="apply.type">
        <el-option label="寝室器材损坏" value="寝室器材损坏"></el-option>
        <el-option label="公共器材损坏" value="公共器材损坏"></el-option>
        <el-option label="更换寝室" value="更换寝室"></el-option>
        <el-option label="更改个人信息" value="更改个人信息"></el-option>
        </el-select>
    </el-form-item>
    <el-form-item label="详情描述">
        <el-input type="textarea" resize="none" rows="8" v-model="apply.infor" placeholder="不超过1000字，个人信息修改请标明要修改的字段"></el-input>
    </el-form-item>
    <el-form-item>
        <el-button type="primary" @click="submitForm()">立即创建</el-button>
        <el-button @click="resetForm()">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
    name:'SendApply',
    data() {
        return {
            apply:{
                sid:JSON.parse(localStorage.getItem('student')).sid,
                type:'寝室器材损坏',
                infor:'',
                date:new Date
            },
            name:JSON.parse(localStorage.getItem('student')).name,
            dorid:JSON.parse(localStorage.getItem('student')).dorid,
            canApply:true
        }
    },
    methods:{
        submitForm(){
            let _this = this
            if(!this.canApply){
                alert('一个人只能申请一次')
                return
            }
            axios.get('http://localhost:8081/apply/addApply',{params:this.apply}).then(function(resp){
                alert('添加成功')
                _this.canApply = false
            }).catch(function(){
                alert('添加失败')
            })
        },
        resetForm(){
            this.apply.type = '寝室器材损坏'
            this.apply.infor = ''
        }
    },
    mounted(){
        let _this = this
        axios.get(`http://localhost:8081/apply/getApplyBySid/${this.apply.sid}`).then(function(resp){
          _this.canApply = resp.data == ''
        })
        let str = this.apply.date.getFullYear()+'-'
        +(this.apply.date.getMonth()+1)+'-'
        +this.apply.date.getDate();
        this.apply.date = str
    }
}
</script>

<style scoped>
.el-form{
    width: 60%;
    padding: 20px;
    border-radius: 5px;
    position: relative;
    background-color: #fff;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    box-shadow: 3px 3px 6px 2px rgba(0, 0, 0, 0.5);
}
</style>